
from libTodotxt import Task, Tasks

def test_Tasks_eq():
    taskList=[
        '(A) Thank Mom for the meatballs @phone',
        '(B) Schedule Goodwill pickup +GarageSale @phone',
        'Post signs around the neighborhood +GarageSale',
        '@GroceryStore Eskimo pies'
       ]
    tasks = Tasks()
    for tstring in taskList:
        tasks.append(Task(tstring))
    for tstring in taskList:
        assert Task(tstring)  in tasks

def test_Tasks_autosort():
    taskList=[
        '(A) Thank Mom for the meatballs @phone',
        '(B) Schedule Goodwill pickup +GarageSale @phone',
        'Post signs around the neighborhood +GarageSale',
        '@GroceryStore Eskimo pies'
       ]
    tasks = Tasks()
    for tstring in taskList:
        tasks.append(Task(tstring))

    assert tasks[0].priority == 'A'
    assert tasks[0].text == 'Thank Mom for the meatballs @phone'
    assert tasks[1].priority == 'B'
    assert tasks[1].text == 'Schedule Goodwill pickup +GarageSale @phone'
    assert tasks[3].priority is None
    assert tasks[3].text == 'Post signs around the neighborhood +GarageSale'


def test_Tasks_comprehension():
    taskList=[
        '(A) Thank Mom for the meatballs @phone',
        '(B) Schedule Goodwill pickup +GarageSale @phone',
        'Post signs around the neighborhood +GarageSale',
        '@GroceryStore Eskimo pies'
       ]
    tasks = Tasks()
    for tstring in taskList:
        tasks.append(Task(tstring))

    for task in [task for task in tasks if task.priority == 'A' ]:
        assert task.toString() == taskList[0]

def test_Tasks_filelikeobj():
    """test Tasks object by instantiating with a file-like object"""
    s = """ccc xxx this line should be third.
aaa zzz this line should be first.
bbb yyy this line should be second."""
    from StringIO import StringIO
    hndl = StringIO(s)
    tasks = Tasks(hndl)
    slist = s.split("\n")
    assert tasks[0].text == slist[1]
    assert tasks[1].text == slist[2]
    assert tasks[2].text == slist[0]

def test_Tasks_AppendExisting():
    """test Tasks object by appending a new task and check sort order"""
    s = """ccc xxx this line should be third.
aaa zzz this line should be first.
bbb yyy this line should be second."""
    from StringIO import StringIO
    hndl = StringIO(s)
    tasks = Tasks(hndl)
    a = '000 xxx this line should end up First'
    tasks.append(Task(a))
    assert tasks[0].text == a
    assert tasks[0].id == 4

def test_Tasks_Enumerate():
    s = """ccc xxx this line should be third.
aaa zzz this line should be first.
bbb yyy this line should be second.
"""
    compareTo = """2 aaa zzz this line should be first.
3 bbb yyy this line should be second.
1 ccc xxx this line should be third."""
    ctList = compareTo.split('\n')

    from StringIO import StringIO
    hndl = StringIO(s)
    tasks = Tasks(hndl)
    ctr = 0
    for num, task in tasks.enumerate():
        assert "%s %s" %( num, task) == ctList[ctr]
        ctr += 1

def test_Tasks_Padding_on_lineno():
    """check that lineno's are zero padded to a common length"""
    s='This is a filler task'
    tasks = Tasks()
    for _ in range(9):
        tasks.append(Task(s))
    for num, _ in tasks.enumerate():
        assert len(num) == 1
    #10's
    tasks.append(Task(s))
    for num, _ in tasks.enumerate():
        assert len(num) == 2
    #100's
    for _ in range(90):
        tasks.append(Task(s))
    for num, _ in tasks.enumerate():
        assert len(num) == 3
